import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common';

import { Request, Response } from 'express';

@Catch(HttpException)
export class CustomHttpException implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    let ctx = host.switchToHttp();
    let req = ctx.getRequest<Request>();
    let res = ctx.getResponse<Response>();
    let status = exception.getStatus();
    console.log(exception);
    let exceptionRes: any = exception.getResponse();
    let { error, message } = exceptionRes;

    res.status(status).json({
      status,
      timestamp: new Date().toISOString(),
      path: req.url,
      error,
      message,
    });
  }
}
